package com.zhuxiang.test5;

import java.util.*;

/**
 * 使用集合实现斗地主游戏的部分功能
 */
public class Demo5 {

    /**
     * 显示扑克牌的方法
     * @param name
     * @param poker
     * @param list
     */
    public static void show(String name,Map<Integer,String> poker,List<Integer> list){
        System.out.print(name+":");
        for (Integer key : list) {
            String value = poker.get(key);
            System.out.print(value+" ");
        }
        System.out.println();

    }
    public static void main(String[] args) {
        //（1）首先准备 54 张扑克牌并打乱顺序。
        Map<Integer,String>  poker = new HashMap<>();

        //存储牌的索引
        List<Integer> pokerIndex = new ArrayList();

        //定义两个数组存储花色和牌的序号
        String[] colors = {"♠","♥","♣","♦"};
        String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};

        //把大小王存储到集合中
        poker.put(1,"大王");
        poker.put(2,"小王");
        int index = 3;
        //循环遍历数组
        for (String num : numbers) {
            for (String color : colors) {
                poker.put(index,color+num);
                index++;
            }
        }

        for (int i = 1; i < 55; i++) {
            pokerIndex.add(i);
        }
        //洗牌
        for (int i = 0; i < 10; i++) {
            Collections.shuffle(pokerIndex);
        }

        //（2）由三个玩家交替摸牌，每人 17 张扑克牌，最后三张留作底牌。
        //定义四个集合，分别代表三个玩家和底牌的索引
        List<Integer> play1 = new ArrayList<>();
        List<Integer> play2 = new ArrayList<>();
        List<Integer> play3 = new ArrayList<>();
        List<Integer> cards = new ArrayList<>();

        for (int i = 0; i < pokerIndex.size(); i++) {
            Integer integer = pokerIndex.get(i);
            //最后三张当作底牌
            if (i >= 51){
                cards.add(integer);
            }else if (0 == i%3){
                //发牌给play1
                play1.add(integer);
            }else if (1 == i%3){
                //发牌给play2
                play2.add(integer);
            }else if (2 == i%3){
                //发牌给play3
                play3.add(integer);
            }
        }

        //排序
        Collections.sort(play1);
        Collections.sort(play2);
        Collections.sort(play3);
        Collections.sort(cards);

        show("play1",poker,play1);
        show("play2",poker,play2);
        show("play3",poker,play3);
        show("底牌",poker,cards);
    }
}
